package com.wyl.search.model;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.servlet.http.HttpServletRequest;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.wyl.search.bean.PaginationBean;
import com.wyl.search.bean.SearchMessageBean;

@Service
@Transactional
public class GeneralSearchService<T> {
	public static String IN_PARAMETER = "_P_";
	
	@PersistenceContext
	private EntityManager entityManager;
	
	@Transactional(readOnly = true)
	@SuppressWarnings("unchecked")
	public PaginationBean getPaginationBean(SearchMessageBean searchMessageBean, int pageNo, Class t){
		PaginationBean bean = new PaginationBean();
		Integer sumRecord = null;
		List<T> dataList = null;
	
		pageNo=pageNo==0?1:pageNo;
		int firstResult=(pageNo-1>=0?pageNo-1:0) * searchMessageBean.getPageSize();
		int maxResult=searchMessageBean.getPageSize();
		
		sumRecord = new Integer(entityManager.createNativeQuery(getCountSql(searchMessageBean.getSql())).getSingleResult().toString());
		if(sumRecord.intValue()>0)
			dataList = entityManager.createNativeQuery(searchMessageBean.getSql(), t).setFirstResult(firstResult).setMaxResults(maxResult).getResultList();
			
		bean.setFormName(searchMessageBean.getFormName());
		bean.setTargetDivName(searchMessageBean.getTargetDivName());
		bean.setSumRecord(sumRecord.intValue());
		bean.setDataList(dataList);
		bean.setCurrentPageNo(pageNo);
		bean.setPageSize(searchMessageBean.getPageSize());				
		bean.setSumPage(sumRecord.intValue()%bean.getPageSize()!=0 ? sumRecord.intValue()/bean.getPageSize()+1 :sumRecord.intValue()/bean.getPageSize());
		bean.setFirstPageNo(1);
		bean.setLastPageNo(bean.getSumPage());		
		bean.setFrontPageNo(bean.getCurrentPageNo()-1<1?1:bean.getCurrentPageNo()-1);
		bean.setBehindPageNo(bean.getCurrentPageNo()+1>bean.getSumPage()?bean.getSumPage():bean.getCurrentPageNo()+1);
		//bean.setColumnName(searchMessageBean.getColumnName().split(","));
		int halfSize = searchMessageBean.getPageNoListSize()/2;
		int startNo = 1;
		int endNo = bean.getLastPageNo();
		
		if(bean.getCurrentPageNo()-halfSize>=1 && bean.getCurrentPageNo()+halfSize <= bean.getLastPageNo()){
			startNo=bean.getCurrentPageNo()-halfSize;
			endNo=startNo+searchMessageBean.getPageNoListSize()-1;
		}else if(bean.getCurrentPageNo()-halfSize<1){
			startNo=1;
			endNo=searchMessageBean.getPageNoListSize()>bean.getLastPageNo()?bean.getLastPageNo():searchMessageBean.getPageNoListSize();
		}else if(bean.getCurrentPageNo()+halfSize > bean.getLastPageNo()){
			endNo=bean.getLastPageNo();
			startNo=endNo-searchMessageBean.getPageNoListSize()>0?endNo-searchMessageBean.getPageNoListSize()+1:1;
		}
		int[] pageNoList = new int[endNo-startNo+1];
		for(int i=0;i<endNo-startNo+1;i++){
			pageNoList[i]=startNo+i;
		}
		bean.setPageNoList(pageNoList);		
		return bean;
	}
	
	/**
	 * 
	 * getRequestParam:(取请求Request的提交参数). <br/>
	 * 这里没有考虑SQL注入.这个后期需要修改
	 * 
	 */
	public Map<String,String> getRequestParam(HttpServletRequest request){
		Map<String,String> result = new HashMap<String,String>();
		Map<String, String[]> params = request.getParameterMap();
		Set<String> keys = params.keySet();
		 for (String key : keys) {
             String tempString = params.get(key)[0];
             result.put(key, tempString);
         }
		return result;
	}
	
	/**
	 * getCountSql:(计算一共有多少条记录). <br/>
	 */
	private String getCountSql(String sql){
		return sql.substring(0,sql.indexOf("SELECT")+6)+ " COUNT(*) "+ sql.substring(sql.indexOf("FROM"));
	}
}
